package cn.netjava.robo3d.device;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import cn.netjava.robo3d.RoboTank;
import cn.netjava.robo3d.TankInformation;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.TriMesh;
import com.jme.system.DisplaySystem;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;

public class Bullet extends Node {

	public static float Maxlife=100f;            //最长寿命
	private static float extime=20f;    //爆炸延时
	private float speed;  //子弹速度
	private Vector3f bulletdirection; //子弹方向
	private Vector3f originalloc;       //子弹初始位置
	private Vector3f currentloc;       //子弹当前位置
	private TankInformation info;
	private float lifetime=Maxlife;           //子弹寿命
	private boolean live;                  //活动状态
	private boolean install=false;              //安装状态
	private TriMesh bulletmodel;
	private Explosion explosion;      //子弹撞车后爆炸
	private boolean bombflag=false; //标志是否爆炸
	private float bombtime=extime;        
	
	public Bullet(TankInformation info,DisplaySystem display){
	   this.setName("bulletnode");
	   explosion=new Explosion();
	   this.info=info;
	   live=false;

	   URL modelurl=RoboTank.class.getClassLoader().getResource("cn/netjava/robo3d/model/bullet.obj");
		FormatConverter converter=new ObjToJme();
       // Point the converter to where it will find the .mtl file from
//       converter.setProperty("mtllib",modelurl);

       // This byte array will hold my .jme file
       ByteArrayOutputStream BO=new ByteArrayOutputStream();
       try {
           // Use the format converter to convert .obj to .jme
           converter.convert(modelurl.openStream(), BO);
           bulletmodel=(TriMesh)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
           // shrink this baby down some
           bulletmodel.setLocalScale(0.04f);
           bulletmodel.setModelBound(new BoundingBox());
           bulletmodel.updateModelBound();
           this.attachChild(bulletmodel);
       } catch (IOException e) {   // Just in case anything happens

           System.exit(0);
       }

		this.updateWorldBound();
	   this.setModelBound(new BoundingSphere());

   }
	//子弹爆炸
	public void bomb(){
		System.out.println("子弹爆炸");
		//设置为爆炸状态
		//子弹先停下
		//爆炸模型替换子弹模型
		bombflag=true;
		this.detachChild(bulletmodel);
		this.attachChild(explosion);
	}
	public Vector3f getBulletdirection() {
		return bulletdirection;
	}
	
	public Vector3f getCurrentloc(){
		return currentloc;
	}

	//返回安装状态
	public boolean getinstall(){
		return install;
	}

	public float getlifetime(){
		return lifetime;
	}
	public Vector3f getOriginalloc() {
		return originalloc;
	}

	public float getSpeed() {
		return speed;
	}
	//返回活动状态
	public boolean islive(){
		return live;
	}

	//激活
	public void setactive(){
		   live=true;
		   lifetime=Maxlife;
		   this.originalloc=info.getLocaltranslation();
		   this.bulletdirection=info.getGundirection().normalize();
		   speed=400f+info.getSpeed()*(float)Math.cos(info.getCasealpha());
		   
		   currentloc=new Vector3f();
		   if(lifetime==Maxlife){
			   currentloc.x=originalloc.x;
			   currentloc.y=originalloc.y+0.7f;
			   currentloc.z=originalloc.z;
		   }
//		   Quaternion qua=new Quaternion();
//		   qua.fromAngleAxis(-FastMath.PI/2, new Vector3f(info.getCardirection()));
////		   this.setLocalRotation(qua);
//		   this.rotateUpTo(bulletdirection);
		   this.setLocalTranslation(currentloc);

	}
	public void setBulletdirection(Vector3f bulletdirection) {
		this.bulletdirection = bulletdirection;
	}
	public void setinstall(boolean install){
		this.install=install;
	}
	public void setlifetime(float lifetime){
		this.lifetime=lifetime;
	}
	public void setOriginalloc(Vector3f originalloc) {
		this.originalloc = originalloc;
	}
	public void setSpeed(float speed) {
		this.speed = speed;
	}
	/**
	 * 更新子弹属性
	 * @param time
	 * @return
	 */
	public void update(float time){
		if(lifetime==Maxlife){
			originalloc=currentloc;
		}
		if(lifetime==0){
			live=false;		
			//生命终结时，模型置换
			this.detachChild(explosion);
			this.attachChild(bulletmodel);
		}
	      	
		  currentloc=currentloc.add(bulletdirection.mult(time*speed));


		  //如果没有爆炸，继续前进
		if(!bombflag){
		     this.setLocalTranslation(currentloc);
		     lifetime--;
		}else{
			//如果爆炸，则停下，而且换成爆炸模型
			
			//爆炸时间过了，设置为非爆炸,换成非爆炸模型
			if(bombtime<=0){
				bombflag=false;
				this.detachChild(explosion);
				this.attachChild(bulletmodel);
				bombtime=extime;
				//爆炸时间一过，子弹生命终结
				lifetime=0;
			}
			bombtime--;
		}

	}
}
